//
//  GYDNewfeatureViewController.swift
//  Weibo
//
//  Created by 贵永冬 on 16/7/26.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class GYDNewfeatureViewController: UICollectionViewController {

    init() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: GYDWindowWidth, height: GYDWindowHeight)
        layout.scrollDirection  =  UICollectionViewScrollDirection.Horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        super.init(collectionViewLayout: layout)
    }
    
    private lazy var imageArray : [String] = {
        return ["new_feature_1","new_feature_2","new_feature_3","new_feature_4"]
    }()
    
    
    private lazy var wbButton : UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        button.setBackgroundImage(UIImage(named:"new_feature_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named:"new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.frame = CGRect(x: 0, y: 0, width: 372/2.0, height: 85/2.0)
        button.center = CGPoint(x: GYDWindowWidth/2.0, y: GYDWindowHeight/4.0*3)
        button.addTarget(self, action: #selector(GYDNewfeatureViewController.gotoWB), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
    @objc private func gotoWB() {
        UIApplication.sharedApplication().keyWindow?.rootViewController = GYDTabBarController()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "lastCellId")
        self.collectionView?.backgroundColor = UIColor.whiteColor()
        self.collectionView?.pagingEnabled = true
        self.collectionView?.bounces = false
        self.collectionView?.showsVerticalScrollIndicator = false
        self.collectionView?.showsHorizontalScrollIndicator = false
    }
    
    // MARK: UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        var cell : UICollectionViewCell?
        
        if indexPath.item < imageArray.count - 1 {
            cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
        }else {
            cell = collectionView.dequeueReusableCellWithReuseIdentifier("lastCellId", forIndexPath: indexPath)
        }
        
        
        var imageView : UIImageView? = cell!.viewWithTag(900) as? UIImageView
        if imageView == nil {
            imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: GYDWindowWidth, height: GYDWindowHeight))
            imageView!.tag = 900
            cell!.addSubview(imageView!)
        }
        let imagePath = NSBundle.mainBundle().pathForResource(imageArray[indexPath.item], ofType: ".png")
        if imagePath != nil {
            imageView!.image = UIImage(contentsOfFile: imagePath!)
        }
        if indexPath.item == imageArray.count - 1 && self.wbButton.superview == nil {
            cell!.addSubview(self.wbButton)
        }
        return cell!
    }
}
